package com.tenoz.display
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	public class DropdownMenu extends Sprite
	{
		public var menu:Menu;
		public var selectedBG:Sprite;
		protected var colors:Array;
		protected var alphas:Array;
		private var open:Boolean = false;
		
		public var textfield:TextField;
		
		public function DropdownMenu()
		{
			
		}
		
		public function init(menuXML:XML):void
		{
			this.menu = new Menu();
			this.menu.setXML(menuXML);
			
			this.textfield.text = menuXML.name;
			this.textfield.autoSize = 'left';
			this.textfield.mouseEnabled = false;
			
			this.buttonMode = true;
			
			this.menu.y = 22;
			this.menu.visible = false;
			this.addChild(this.menu);
			
//			this.addEventListener(MouseEvent.CLICK, this.toggleOpen);
		}
		
		public function onResize(stageWidth:Number, stageHeight:Number):void
		{
			this.menu.onResize(stageWidth, stageHeight);
			if(stageWidth < this.menu.width + this.x + 10)
			{
				this.menu.x = stageWidth - this.x - this.menu.width - 10;
			}
			else
			{
				this.menu.x = 0;
			}
		}
		
		public function getWidth():Number
		{
			return this.selectedBG.width;
		}
		
		public function toggleOpen(e:MouseEvent = null):void
		{
			if(open)
			{
				closeMenu();
			}
			else
			{
				openMenu();
			}
		}
		
		public function openMenu():void
		{
			this.menu.visible = true;
			this.open = true;
		}
		
		public function closeMenu():void
		{
			this.menu.visible = false;
//			this.menu.sliderV.slider.setValue(0);
			
			this.open = false;
		}
	}
}